<?php 
/*
**********************************************
JoomLeague RESULTS TABS MODULE
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* RESULTS TABS MODULE
*
* 2007-07-31 - Version 0.90
*
* Displays the last, current and next matches and results in 3 single tabs.
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://www.joomleague.de//
**********************************************
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if (file_exists($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php')) {
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php');
} 
else{
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/english.php');
}

require_once 'administrator/components/com_joomleague/joomleague.class.php';
require_once 'components/com_joomleague/js/joomleague_tabs.js.php';?>

<!-- To remain XHTML compatibility move this stylesheet link to the head of your index.php -->
<link rel="stylesheet" type="text/css" href="<?php echo $mosConfig_live_site;?>/components/com_joomleague/css/joomleague_tabs.css" />
<!-- Stylesheet link END -->

<?php 
#comment this line for mambo version < 4.5.1
$params = mosParseParams( $module->params );

$p = $params->project; //default 0
$show = $params->show; //default 0

if ($params->maxround_auto == 1){
// Query max. round if set to automatic
$query = "SELECT matchcode
          FROM #__joomleague_rounds
          WHERE project_id = 1
          ORDER BY matchcode DESC LIMIT 1";
$database->setQuery($query);
$maxround = $database->loadResult();   
//Query max. round END
}
else $maxround = $params->maxround;
$project = new JoomLeague( $database );
$project->load($p);
$current_round = $project->current_round;

$rounds = array();
if($current_round > 1) $rounds[] = $current_round - 1;
$rounds[] = $current_round;
if($current_round != $maxround) $rounds[] = $current_round + 1;

?>

<?php if($show > 0){?><div align="center"><b><?php echo $project->name;?></b></div><?php }?>


<script language="javascript" type="text/javascript">
<!-- // --><![CDATA[ 
var dhtml<?php echo $module->id; ?> = new mosDHTML();
// ]]></script>

<table cellspacing="0" cellpadding="1" border="0" width="100%">
  <tr align="center">
    
<?php 
    $counter = 0;
    $c = count($rounds);
    $width = floor(100/$c);
    foreach($rounds as $round){
?>
      <td width="<?php echo $width;?>%" id="tab<?php echo $round.$module->id; ?>" class="offtab" onclick="dhtml<?php echo $module->id; ?>.cycleTab(this.id)"><?php echo $round;?>.<br /><?php echo _JL_MATCHDAY;?></td> 
<?php	

    } 
?>
  
   
 </tr>
</table>
  
<?php 
    $counter = 0;
    $JScrA="";
    foreach($rounds as $round){
    	if ($round!=$current_round) $JScrA.="dhtml".$module->id.".cycleTab('tab".$round.$module->id."');\n";
      $matches = $project->getResults($round);
?>
      <div id="page<?php echo $round.$module->id; ?>" class="pagetext">
        <table cellspacing="1" cellpadding="1" border="0" width="100%">
          <tr><td colspan="3" align="center"><b>

          </b></td></tr>
<?php
					$favteams = explode(",", $project->fav_team);
          $k = 0;
          foreach($matches as $v){
					$match = new Match ($database);
					$match->load($v->match_id);
					$hometeam = new JL_Team($database);
					$hometeam->load($match->matchpart1);
					$guestteam = new JL_Team($database);
					$guestteam->load($match->matchpart2);
          $class = ($k == 0)? 'sectiontableentry1' : 'sectiontableentry2';
					if (is_null($match->matchpart1_result)) $goals1="-"; else $goals1=$match->matchpart1_result;
					if (is_null($match->matchpart2_result)) $goals2="-"; else $goals2=$match->matchpart2_result;
		      if ($match->match_result_type == "1") { 
            $mrt = "#00E3FF";
            
            if(in_array($hometeam->id, $favteams)){ ;?>
              
              <tr class="<?php print $class;?>">
                <td align="right"><b><?php echo $hometeam->short_name ;?></b></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
            elseif(in_array($guestteam->id, $favteams)){ ;?>
    
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><b><?php echo $guestteam->short_name;?></b></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
            else{ ?>
            
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
          }
          elseif ($match->match_result_type == "2"){  
            $mrt = "#FFDF00";
            
            if(in_array($hometeam->id, $favteams)) { ?>
            
              <tr class="<?php print $class;?>">
                <td align="right"><b><?php echo $hometeam->short_name ;?></b></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
            elseif(in_array($guestteam->id, $favteams)){ ?>
              
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><b><?php echo $guestteam->short_name;?></b></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }else{  ?>
  
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center" bgcolor="<?php print $mrt;?>"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
	        }
          else{
            
            if(in_array($hometeam->id, $favteams)){ ;?>
            
              <tr class="<?php print $class;?>">
                <td align="right"><b><?php echo $hometeam->short_name ;?></b></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }
            elseif(in_array($guestteam->id, $favteams)){ ;?>
    
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><b><?php echo $guestteam->short_name;?></b></td>
                <td align="center"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>
<?php       }else{  ?>
  
              <tr class="<?php print $class;?>">
                <td align="right"><?php echo $hometeam->short_name ;?></td>
                <td>-</td>
                <td align="left"><?php echo $guestteam->short_name;?></td>
                <td align="center"><?php echo sprintf("%s:%s",$goals1,$goals2);?></td>
              </tr>

   <?php    } 
       
          }
       
          $k = 1 - $k;
        }
?>

        </table>
      </div>
<?php } ?>


<script language="javascript" type="text/javascript">
<?
echo $JScrA;
echo "dhtml".$module->id.".cycleTab('tab".$current_round.$module->id."');\n";
?>
</script>
